package pro.seac.twitter.web.view;

import static pro.seac.twitter.application.MessageService.MAX_MESSAGE_LENGTH;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Component;

import pro.seac.twitter.application.MessageService;
import pro.seac.twitter.application.dto.MessageData;
import pro.seac.twitter.web.Navigateable;
import pro.seac.twitter.web.view.component.MessageList;

import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

@Component
@Scope("prototype")
@Navigateable("home")
@Secured("ROLE_USER")
public class UserHomeView extends BaseView {
	private static final long serialVersionUID = 1L;
	@Autowired
	private MessageService service;
	private TextArea editor;
	private MessageList messagesList;

	@Override
	public void enter(ViewChangeEvent event) {
		super.enter(event);
		refreshMessages();
	}

	protected com.vaadin.ui.Component initContent() {
		VerticalLayout container = (VerticalLayout) super.initContent();
		container.setSizeFull();
		Label label = null;
		container.addComponent(label = new Label("Dodaj nowy wpis", ContentMode.HTML));
		label.setWidth(100, Unit.PERCENTAGE);
		label.setStyleName(Reindeer.LABEL_SMALL);
		editor = new com.vaadin.ui.TextArea();
		editor.setRows(3);
		editor.setWidth("100%");
		// TODO Dodaj licznik ile jeszcze pozostalo znakow do wprowadzenia
		editor.setMaxLength(MAX_MESSAGE_LENGTH);
		container.addComponent(editor);
		Button button;
		container.addComponent(button = new Button("publikuj"));
		button.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				publishMessage();
			}
		});
		container.setComponentAlignment(button, Alignment.MIDDLE_RIGHT);
		Panel messagesContainer = new Panel();
		messagesContainer.setStyleName(Reindeer.PANEL_LIGHT);
		messagesContainer.setSizeFull();
		messagesList = new MessageList();
		messagesContainer.setContent(messagesList);

		container.addComponent(messagesContainer);
		container.setExpandRatio(messagesContainer, 1);
		return container;
	}

	protected String getTitle() {
		return "Strona domowa: " + getCurrentUser().getUsername();
	}

	public void publishMessage() {
		String content = editor.getValue();
		if (StringUtils.isBlank(content)) {
			Notification.show("B\u0142\u0105d", "Wpisz wiadomo\u015B\u0107", Type.WARNING_MESSAGE);
		} else if (content.length() > MAX_MESSAGE_LENGTH) {
			Notification.show("B\u0142\u0105d", "Wiadomo\u015B\u0107 za d\u0142uga, max: " + MAX_MESSAGE_LENGTH + " znak\u00F3w", Type.WARNING_MESSAGE);
		} else {
			try {
				service.publishMessage(getCurrentUser().getId(), content);
				Notification.show("Sukces", "Wiadomo\u015B\u0107 zosta\u0142a opublikowana", Type.HUMANIZED_MESSAGE);
				editor.setValue(StringUtils.EMPTY);
			} catch (Exception e) {
				Notification.show("B\u0142\u0105d", "Wyst\u0105pi\u0142 b\u0142\u0105d: " + e.getMessage() + "!", Type.ERROR_MESSAGE);
			}
		}
	}

	public void refreshMessages() {
		List<MessageData> messages = service.getUserHomeFeedMessages(getCurrentUser().getId());
		messagesList.refreshMessages(messages);
	}
}
